package game.entities;

import game.Game;
import game.GameEvents;
import game.InputHandler;
import game.gfx.Colours;
import game.gfx.Screen;
import level.Level;

public class Player extends Mob
{
	private InputHandler input;
	private int colour = Colours.get(-1, 111, 145, 543);
	private int scale = 1;
	protected boolean isSwimming = false;
	public boolean triggerPulled = false;
		
	private int muzzleFlashCounter = 0;
	private int[] keyPressedCounter = new int[4];
	private int minVal = 9999;
	private int minValIndex = -1;
	
	public static int xPlayer;
	public static int yPlayer;
	public static int playerDir;
	
	Bullet bullet;
		
	public Player(Level level, int x, int y, InputHandler input)
	{
		super(level, "Player", x, y, 1);
		this.input = input;
	}

	public void tick()
	{
		int xa = 0;
		int ya = 0;
		
		if (input.up.isPressed()) keyPressedCounter[0]++;
		if (input.down.isPressed()) keyPressedCounter[1]++;
		if (input.left.isPressed()) keyPressedCounter[2]++;
		if (input.right.isPressed()) keyPressedCounter[3]++;
		
		if (!input.up.isPressed()) keyPressedCounter[0] = 0;
		if (!input.down.isPressed()) keyPressedCounter[1] = 0;
		if (!input.left.isPressed()) keyPressedCounter[2] = 0;
		if (!input.right.isPressed()) keyPressedCounter[3] = 0;
		
		minVal = 9999;
		for (int i = 0; i < 4; i++)
		{
			if ((keyPressedCounter[i] > 0) && (keyPressedCounter[i] < minVal))
			{
				minVal = keyPressedCounter[i];
				minValIndex = i;
			}
		}		
		
		if (minVal == 9999) minValIndex = -1;
		else if (minValIndex == 0) ya--;
		else if (minValIndex == 1) ya++;
		else if (minValIndex == 2) xa--;
		else if (minValIndex == 3) xa++;
							
		if (xa != 0 || ya != 0)
		{
			move(xa, ya);
			isMoving = true;
		}
		else isMoving = false;

		if (input.shoot.isPressed())
		{
			triggerPulled = true;
			Bullet.bulletFired = true;
		}
	}

	public void render(Screen screen)
	{
		int xTile = 8;
		int yTile = 28;
		int walkingSpeed = 3;
		
		int playerModifier = 8 * scale;
		int xOffset = x - playerModifier / 2;
		int yOffset = y - playerModifier / 2 - 4;
		
		this.xPlayer = x;
		this.yPlayer = y;
		this.playerDir = movingDir;
		
		if (movingDir == 0) // Up
		{
			screen.render(xOffset, yOffset, (xTile) + (yTile) * 32, colour, flipNone, scale);
			screen.render(xOffset + playerModifier, yOffset, (xTile) + (yTile) * 32, colour, flipX, scale);
			screen.render(xOffset, yOffset + playerModifier, (xTile) + (yTile + 1) * 32, colour, flipNone, scale);
			screen.render(xOffset + playerModifier, yOffset + playerModifier, (xTile) + (yTile + 1) * 32, colour, flipX, scale);
		}
		else if (movingDir == 1)  // Down
		{
			screen.render(xOffset, yOffset, (xTile) + (yTile + 1) * 32, colour, flipY, scale);
			screen.render(xOffset + playerModifier, yOffset, (xTile) + (yTile + 1) * 32, colour, flipXY, scale);
			screen.render(xOffset, yOffset + playerModifier, (xTile) + (yTile) * 32, colour, flipY, scale);
			screen.render(xOffset + playerModifier, yOffset + playerModifier, (xTile) + (yTile) * 32, colour, flipXY, scale);
		}
		else if (movingDir == 2)  // Left
		{
			screen.render(xOffset, yOffset, (xTile + 2) + (yTile) * 32, colour, flipX, scale);
			screen.render(xOffset + playerModifier, yOffset, (xTile + 1) + (yTile) * 32, colour, flipX, scale);
			screen.render(xOffset, yOffset + playerModifier, (xTile + 2) + (yTile) * 32, colour, flipXY, scale);
			screen.render(xOffset + playerModifier, yOffset + playerModifier, (xTile + 1) + (yTile) * 32, colour, flipXY, scale);
		}
		else if (movingDir == 3)  // Right
		{
			screen.render(xOffset, yOffset, (xTile + 1) + (yTile) * 32, colour, flipNone, scale);
			screen.render(xOffset + playerModifier, yOffset, (xTile + 2) + (yTile) * 32, colour, flipNone, scale);
			screen.render(xOffset, yOffset + playerModifier, (xTile + 1) + (yTile) * 32, colour, flipY, scale);
			screen.render(xOffset + playerModifier, yOffset + playerModifier, (xTile + 2) + (yTile) * 32, colour, flipY, scale);
		}
		
		if (triggerPulled)
		{
			int muzzleFlashColour;
			int muzzleFlashLength = 20;
			
			if (muzzleFlashCounter < muzzleFlashLength) muzzleFlashColour = Colours.get(-1, -1, 200, 300);
			else if (muzzleFlashLength <= muzzleFlashCounter && muzzleFlashCounter < muzzleFlashLength * 2) muzzleFlashColour = Colours.get(-1, 225, 115, 300);
			else if (muzzleFlashLength * 2 <= muzzleFlashCounter && muzzleFlashCounter < muzzleFlashLength * 3) muzzleFlashColour = Colours.get(-1, 115, -1, 225);
			else if (muzzleFlashLength * 3 <= muzzleFlashCounter && muzzleFlashCounter < muzzleFlashLength * 4) muzzleFlashColour = Colours.get(-1, 225, 115, 300);
			else
			{
				triggerPulled = false;
				muzzleFlashCounter = 0;
				muzzleFlashColour = Colours.get(-1, -1, -1, -1);
			}
			
			muzzleFlashCounter++;
			
			if (movingDir == 0) screen.render(xOffset + 4, yOffset - 8, 3 + 5 * 32, muzzleFlashColour, flipNone, 1); // Up
			else if (movingDir == 1) screen.render(xOffset + 4, yOffset + 16, 3 + 5 * 32, muzzleFlashColour, flipY, 1); // Down
			else if (movingDir == 2) screen.render(xOffset - 8, yOffset + 4, 4 + 5 * 32, muzzleFlashColour, flipX, 1); // Left
			else if (movingDir == 3) screen.render(xOffset + 16, yOffset + 4, 4 + 5 * 32, muzzleFlashColour, flipNone, 1); // Right
		}
	}
	
	public static int getXPos()
	{
		return xPlayer;
	}
	
	public static int getYPos()
	{
		return yPlayer;
	}
	
	public static int getDir()
	{
		return playerDir;
	}

	public boolean hasCollided(int xa, int ya)
	{
		int xMin = -2;
		int xMax = 9;
		int yMin = -6;
		int yMax = 5;
		
		for (int x = xMin; x < xMax; x++)
		{
			if (isSolidTile(xa, ya, x, yMin))
			{
				return true;
			}
		}
		for (int x = xMin; x < xMax; x++)
		{
			if (isSolidTile(xa, ya, x, yMax))
			{
				return true;
			}
		}
		for (int y = yMin; y < yMax; y++)
		{
			if (isSolidTile(xa, ya, xMin, y))
			{
				return true;
			}
		}
		for (int y = yMin; y < yMax; y++)
		{
			if (isSolidTile(xa, ya, xMax, y))
			{
				return true;
			}
		}
		return false;
	}
		
	public static void removeBullet()
	{
		if (!Bullet.moving)
		{
			level.removeEntity(Game.bullet);
			Bullet.bulletFired = false;
		}
	}
}
